//
//  AdviceFeedbackVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/9.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AdviceFeedbackVC.h"
#import "UITextView+ZWPlaceHolder.h"
#import "TXPingTaiRedCell.h"
#import "ZTXMeRedCell.h"
#import "ZTXRedBottomCell.h"
#import "ZTXChuLiHeaderCell.h"
@interface AdviceFeedbackVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *orderDetailCode;
@property (nonatomic, strong) NSString *parentCode;
@property (nonatomic, strong) NSString *contentStr;
@property (nonatomic, strong) NSString *orderCode;
@property (nonatomic, strong) NSMutableDictionary *headerdic;
@property (nonatomic, strong) NSString *headerContentStr;

@property (weak, nonatomic) IBOutlet UIButton *btnSave;
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (nonatomic,strong) FlexTextView *txtView;
@end

@implementation AdviceFeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"投诉意见反馈")];
    [self setBackLeftButton:@""];
    self.textview.textColor = [UIColor cjMainTextColor];
    self.btnSave.backgroundColor = [UIColor cjMainColor];
    self.textview.hidden = YES;
    self.btnSave.hidden = YES;
    [self confirmButtonClick];
    self.dataArray = [@[] mutableCopy];
    self.headerdic = [NSMutableDictionary dictionary];
    self.view.backgroundColor = BR_RGB_HEX(0xf1f1f1, 1.0f);
    [self.view addSubview:self.tableView];
   
    // 请填写产品意见，我们将不断优化体验
    self.textview.placeholder = @"  请填写产品意见，我们将不断优化体验";
    self.textview.zw_placeHolderColor = [UIColor cjSubheadTextColor];
}

//- (IBAction)btnSaveAction:(id)sender {
//    
//    if ([CJRegular getIsStrEmpty:self.textview.text]) {
//        [CJAlert showStringDismissWithDelay:@"请填写反馈内容"];
//    }else{
//        
//        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/feedback",CJBaseUrl);
//        NSDictionary *dicPar = @{
//            @"content":self.textview.text
//        };
//        
//        WS(ws);
//        [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strURL parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
//            [ws pop];
//        } failure:^(NSError * _Nonnull error) {
//            
//        }];
//    }
//}
- (void)confirmButtonClick {

        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/feedback/%@",CJBaseUrl,[CJControl getPartyCode]);

        WS(ws);
        [[HDNetworking sharedHDNetworking] GET:strURL parameters:@{} success:^(id  _Nonnull responseObject) {
            NSDictionary *dict = responseObject[0];
            [ws.dataArray addObjectsFromArray:dict[@"childRenVoList"]];
            ws.code = dict[@"code"];
            ws.parentCode = dict[@"parentCode"];
            ws.headerContentStr = dict[@"content"];
            [ws.headerdic setValue:dict[@"messager"] forKey:@"handler"];
            [ws.headerdic setValue:dict[@"gmtCreated"] forKey:@"gmtCreated"];
            [ws.headerdic setValue:dict[@"content"] forKey:@"handleIdea"];
            [ws.tableView reloadData];
            [ws.tableView scrollToRow:0 inSection:2 atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];
        } failure:^(NSError * _Nonnull error) {
            
        }];
//            [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strURL parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
//                [ws pop];
//            } failure:^(NSError * _Nonnull error) {
//    
//            }];
}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, kTopHeight + 10, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = BR_RGB_HEX(0xf1f1f1, 1.0f);
        //注册单元格
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXChuLiHeaderCell" bundle:nil] forCellReuseIdentifier:@"ZTXChuLiHeaderCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"TXPingTaiRedCell" bundle:nil] forCellReuseIdentifier:@"TXPingTaiRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXMeRedCell" bundle:nil] forCellReuseIdentifier:@"ZTXMeRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXRedBottomCell" bundle:nil] forCellReuseIdentifier:@"ZTXRedBottomCell"];
        
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (self.headerContentStr.length == 0 || ![self.headerContentStr isNotBlank]) {
            return 0;
        }else{
            return 1;
        }
        
    }else if (section == 1){
        return self.dataArray.count;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell fuzhiFanKZTXMeRedCell:self.headerdic];
        return cell;
    }else if (indexPath.section == 1){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            TXPingTaiRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TXPingTaiRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiFanKZTXMeRedCell:dic];
            return cell;
        }else{
            ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiFanKZTXMeRedCell:dic];
            return cell;
        }
    }else{
        ZTXRedBottomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXRedBottomCell" forIndexPath:indexPath];
        [cell.huifuButton addTarget:self action:@selector(huifuButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.txtView = cell.txtView;
        cell.txtView.placeholder = @"请填写产品意见，我们将不断优化体验";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
   
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return [ZTXMeRedCell fuzhiDicHeight:self.headerdic];
    }else if (indexPath.section == 1){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            return [TXPingTaiRedCell fuzhiDicHeight:dic];
        }else{
            return [ZTXMeRedCell fuzhiDicHeight:dic];
        }
    }else if (indexPath.section == 2){
        return 200;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)huifuButtonAction:(UIButton *)btn{
    CGPoint point = btn.center;//找到btn中心点记录下来
    point = [self.tableView convertPoint:point fromView:btn.superview];
    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
    ZTXRedBottomCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
    [self huifuYiChang:cell];
  
}
- (void)huifuYiChang:(ZTXRedBottomCell *)cell{
    if (self.txtView.text.length == 0) {
        [CJAlert showStringDismissWithDelay:@"请填输入回复内容"];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.code forKey:@"code"];
    [params setValue:self.code forKey:@"parentCode"];
    [params setValue:self.txtView.text forKey:@"handleIdea"];
    NSString *urlString = CJStringWithFormat(@"%@/feedback/handle", [NSString stringWithFormat:@"%@/ntocc-basic-api",CJBaseUrl]);

    [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:urlString parameters:params status:OperationTip success:^(id  _Nonnull responseObject) {
        cell.txtView.text = @"";
        [self confirmButtonClick];
    } failure:^(NSError * _Nonnull error) {

    }];
}

@end
